package com.henry.chatkit.messages.holder

import android.view.View
import android.widget.*
import com.henry.chatkit.messages.MessageInfo
import com.henry.chatkit.R
import com.henry.chatkit.MessageStatus
import com.henry.chatkit.glide.GlideEngine
import com.henry.chatkit.utils.ImageUtils

abstract class MessageContentHolder(itemView: View) : MessageEmptyHolder(itemView) {

    var leftUserIcon: ImageView = itemView.findViewById(R.id.left_user_icon_view)
    var rightUserIcon: ImageView = itemView.findViewById(R.id.right_user_icon_view)
    var usernameText: TextView = itemView.findViewById(R.id.user_name_tv)
    var msgContentLinear: LinearLayout = itemView.findViewById(R.id.msg_content_ll)
    var sendingProgress: ProgressBar = itemView.findViewById(R.id.message_sending_pb)
    var statusImage: ImageView = itemView.findViewById(R.id.message_status_iv)
    var unreadAudioText: TextView = itemView.findViewById(R.id.audio_unread)

    override fun layoutViews(msg: MessageInfo?, position: Int) {
        super.layoutViews(msg, position)
        if (msg == null) {
            return
        }

        //// 头像设置
        if (msg.isSelf) {
            leftUserIcon.visibility = View.GONE
            rightUserIcon.visibility = View.VISIBLE
        } else {
            leftUserIcon.visibility = View.VISIBLE
            rightUserIcon.visibility = View.GONE
        }
        if (properties.avatarId != 0) {
            leftUserIcon.setImageResource(properties.avatarId)
            rightUserIcon.setImageResource(properties.avatarId)
        }

        // TODO 圆角 properties.getAvatarRadius()
        if (properties.avatarSize.size == 2) {
            var params = leftUserIcon.layoutParams
            params.width = properties.avatarSize[0]
            params.height = properties.avatarSize[1]
            leftUserIcon.layoutParams = params
            params = rightUserIcon.layoutParams
            params.width = properties.avatarSize[0]
            params.height = properties.avatarSize[1]
            rightUserIcon.layoutParams = params
        }

        //// 用户昵称设置
        if (msg.isSelf) { // 默认不显示自己的昵称
            if (properties.rightNameVisibility == 0) {
                usernameText.visibility = View.GONE
            } else {
                usernameText.visibility = properties.rightNameVisibility
            }
            if (usernameText.visibility == View.GONE) {
                val params = msgContentLinear.layoutParams as RelativeLayout.LayoutParams
                msgContentLinear.layoutParams = params
            }
        } else {
            if (properties.leftNameVisibility == 0) {
                if (msg.isGroup) { // 群聊默认显示对方的昵称
                    usernameText.visibility = View.VISIBLE
                } else { // 单聊默认不显示对方昵称
                    usernameText.visibility = View.GONE
                }
            } else {
                usernameText.visibility = properties.leftNameVisibility
            }
            if (usernameText.visibility == View.GONE) {
                val params = msgContentLinear.layoutParams as RelativeLayout.LayoutParams
                msgContentLinear.layoutParams = params
            }
        }
        if (properties.nameFontColor != 0) {
            usernameText.setTextColor(properties.nameFontColor)
        }
        if (properties.nameFontSize != 0) {
            usernameText.textSize = properties.nameFontSize.toFloat()
        }
        // TODO 聊天界面设置头像和昵称
        usernameText.text = msg.fromUserName

        val icon = ImageUtils.getUserIconPath(msg.fromUserId, msg.fromUserName, msg.fromUserIcon)

        if (msg.isSelf) {
            GlideEngine.loadRoundImage(icon, rightUserIcon, properties.avatarRadius, properties.avatarId)
        } else {
            GlideEngine.loadRoundImage(icon, leftUserIcon, properties.avatarRadius, properties.avatarId)
        }

        if (msg.status == MessageStatus.SENDING || msg.status == MessageStatus.DOWNLOADED) {
            sendingProgress.visibility = View.VISIBLE
        } else {
            sendingProgress.visibility = View.GONE
        }

        //// 聊天气泡设置
        if (msg.isSelf) {
            if (properties.rightBubble != null) {
                msgContentFrame.background = properties.rightBubble
            } else {
                msgContentFrame.setBackgroundResource(R.drawable.chat_bubble_myself)
            }
        } else {
            if (properties.leftBubble != null) {
                msgContentFrame.background = properties.leftBubble
            } else {
                msgContentFrame.setBackgroundResource(R.drawable.chat_bubble_other)
            }
        }


        //// 聊天气泡的点击事件处理
        if (onItemClickListener != null) {
            msgContentFrame.setOnClickListener {
                onItemClickListener?.onMessageClick(it, position, msg)
            }
            msgContentFrame.setOnLongClickListener { v ->
                onItemClickListener?.onMessageLongClick(v, position, msg)
                true
            }
            leftUserIcon.setOnClickListener { view ->
                onItemClickListener?.onUserIconClick(view, position, msg)
            }
            rightUserIcon.setOnClickListener { view ->
                onItemClickListener?.onUserIconClick(view, position, msg)
            }
        }

        //// 发送状态的设置
        if (msg.status == MessageStatus.SEND_FAIL) {
            statusImage.visibility = View.VISIBLE
            statusImage.setOnClickListener {
                onItemClickListener?.onMessageLongClick(msgContentFrame, position, msg)
            }
        } else {
            statusImage.visibility = View.GONE
        }

        //// 左右边的消息需要调整一下内容的位置
        if (msg.isSelf) {
            msgContentLinear.removeView(msgContentFrame)
            msgContentLinear.addView(msgContentFrame)
        } else {
            msgContentLinear.removeView(msgContentFrame)
            msgContentLinear.addView(msgContentFrame, 0)
        }
        msgContentLinear.visibility = View.VISIBLE

        //// 音频已读
        unreadAudioText.visibility = View.GONE

        //// 由子类设置指定消息类型的views
        layoutVariableViews(msg, position)

    }

    abstract fun layoutVariableViews(msg: MessageInfo?, position: Int)

}